// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Page.java

package ui;

import java.util.Vector;
import javax.microedition.lcdui.*;

// Referenced classes of package ui:
//            MenuBar, Element, BrowserCanvas, PageListener

 public class Page
    implements Runnable
{

    public Page(BrowserCanvas canvas, int w, int h)
    {
        SCROLL_HEIGHT = BrowserCanvas.FONTSMALL.getHeight() * 2;
        bgcolor = 0x481bf;
        this.canvas = canvas;
        items = new Vector();
        top = 0;
        screen_height = h;
        width = w;
        selected = -1;
        top_index = 0;
        buttom_index = 0;
        animated = false;
        SCROLL_HEIGHT = (h / BrowserCanvas.FONTSMALL.getHeight() - 1) * BrowserCanvas.FONTSMALL.getHeight();
//        if(icon_up == null)
//            try
//            {
//                icon_up = Image.createImage("/images/up.png");
//                icon_down = Image.createImage("/images/down.png");
//            }
//            catch(IOException e)
//            {
//                e.printStackTrace();
//            }
    }

    public void setListener(PageListener listener)
    {
        this.listener = listener;
        if(left_menu != null && (left_menu instanceof MenuBar))
        {
            MenuBar new_name = (MenuBar)left_menu;
            new_name.listener = listener;
        }
    }

    protected void drawPage(Graphics g, int left, int startY, int stopY)
    {
        if(items.size() == 0)
        {
            g.setColor(0);
            Font f = g.getFont();
            String loading = "Loading....";
            int w = f.stringWidth(loading);
            g.drawString(loading, width - w >> 1, screen_height >> 1, 20);
            return;
        }
        System.out.println("Page.drawPage():" + buttom_index + "," + items.size());
        for(int i = top_index; i < buttom_index; i++)
        {
            Element itm = (Element)items.elementAt(i);
            itm.draw(g, left, top + startY, startY, stopY, i == selected);
        }

        System.out.println("full_screen : " + full_screen);
        if(full_screen > 211)
        {
            if(!dontdrawscrollbar) {
                g.setColor(0);
                int bar_height = (screen_height * screen_height) / full_screen;
                int y = startY - (top * screen_height) / full_screen;
                g.drawLine(g.getClipWidth()+ 25, startY, g.getClipWidth()+25, startY + screen_height);
                g.setColor(0xffffff);
                g.drawLine(g.getClipWidth() + 24, startY, g.getClipWidth() + 24, startY + screen_height);
                g.setColor(0x000000);
                g.fillRect(g.getClipWidth() + 24, y, 2, bar_height);
            }
        }
    }

    public int draw(Graphics g, int left, int startY, int stopY)
    {
        int scroll = 0;
        if(anim_div != 0)
        {
            int y = anim_y + startY;
            if(anim_div < 0)
            {
                g.drawImage(img_cur, left, y, 20);
                g.drawImage(img_next, left, y + img_cur.getHeight(), 20);
            } else
            {
                g.drawImage(img_next, left, y, 20);
                g.drawImage(img_cur, left, y + img_next.getHeight(), 20);
            }
        } else
        {
            drawPage(g, left, startY, stopY);
//            drawPage(g, left, 20, 220);

        }
        if(top < 0)
            scroll |= 1;
        if(full_screen > screen_height && screen_height - top < full_screen)
            scroll |= 2;
        return scroll;
    }

    public boolean keyPressed(int keyCode)
    {
        if(anim_div != 0)
            return false;
        int s = items.size();
        if(s == 0)
            return false;
        if(selected >= 0)
        {
            Element e = (Element)items.elementAt(selected);
            if(e.eventable && e.keyPress(keyCode))
                return true;
        }
        if(keyCode == BrowserCanvas.KEYCENTER)
        {
            if(selected >= 0)
            {
                Element e = (Element)items.elementAt(selected);
                if(e.clickable && listener != null)
                    listener.onItemClick(this, e, 0);
            }
        } else
        if(keyCode == BrowserCanvas.KEYDOWN || keyCode == BrowserCanvas.TRIGHT)
        {
            next_selected = selected;
            int i = selected < 0 ? top_index : selected + 1;
            Element e;
            do
            {
                if(i >= buttom_index)
                    break;
                e = (Element)items.elementAt(i);
                if(e.selectable)
                {
                    next_selected = i;
                    break;
                }
                i++;
            } while(true);
            e = (Element)items.elementAt(s - 1);
            if(i >= buttom_index && top + e.top + e.height > screen_height)
            {
                next_top = top - SCROLL_HEIGHT;
                next_topidx = top_index;
                do
                {
                    if(next_topidx >= s)
                        break;
                    e = (Element)items.elementAt(next_topidx);
                    if(next_top + e.top + e.height > 1)
                        break;
                    next_topidx++;
                } while(true);
                next_buttom = buttom_index;
                do
                {
                    if(next_buttom >= s)
                        break;
                    e = (Element)items.elementAt(next_buttom);
                    if(next_top + e.top > screen_height - 2)
                        break;
                    next_buttom++;
                } while(true);
                i = next_selected >= 0 ? next_selected + 1 : top_index;
                do
                {
                    if(i >= next_buttom)
                        break;
                    e = (Element)items.elementAt(i);
                    if(e.selectable)
                    {
                        next_selected = i;
                        break;
                    }
                    i++;
                } while(true);
            } else
            if(selected == next_selected)
            {
                return false;
            } else
            {
                selected = next_selected;
                return true;
            }
            if(animated)
            {
                anim_y = -1;
                (new Thread(this)).start();
            } else
            {
                top = next_top;
                top_index = next_topidx;
                buttom_index = next_buttom;
                selected = next_selected;
                return true;
            }
        } else
        if(keyCode == BrowserCanvas.KEYUP || keyCode == BrowserCanvas.TLEFT)
        {
            next_selected = selected;
            int i = selected >= 0 ? selected - 1 : buttom_index - 1;
            do
            {
                if(i < top_index)
                    break;
                Element e = (Element)items.elementAt(i);
                if(e.selectable)
                {
                    next_selected = i;
                    break;
                }
                i--;
            } while(true);
            if(top < 0 && i < top_index)
            {
                next_top = top + SCROLL_HEIGHT;
                next_topidx = top_index;
                do
                {
                    if(next_topidx <= 0)
                        break;
                    Element e = (Element)items.elementAt(next_topidx);
                    if(next_top + e.top <= 1)
                        break;
                    next_topidx--;
                } while(true);
                next_buttom = buttom_index;
                if(next_buttom >= s)
                {
                    Element e = (Element)items.elementAt(s - 1);
                    if(next_top + e.top + e.height > screen_height)
                        next_buttom = s - 1;
                }
                if(next_buttom < s)
                {
                    do
                    {
                        if(next_buttom < next_topidx)
                            break;
                        Element e = (Element)items.elementAt(next_buttom);
                        if(next_top + e.top < screen_height)
                            break;
                        next_buttom--;
                    } while(true);
                    next_buttom++;
                }
                i = next_selected >= 0 ? next_selected - 1 : buttom_index - 1;
                do
                {
                    if(i < next_topidx)
                        break;
                    Element e = (Element)items.elementAt(i);
                    if(e.selectable)
                    {
                        next_selected = i;
                        break;
                    }
                    i--;
                } while(true);
            } else
            if(next_selected == selected)
            {
                return false;
            } else
            {
                selected = next_selected;
                return true;
            }
            if(animated)
            {
                anim_y = 1;
                (new Thread(this)).start();
            } else
            {
                top = next_top;
                top_index = next_topidx;
                buttom_index = next_buttom;
                selected = next_selected;
                return true;
            }
        }
        return false;
    }

    public void clear()
    {
        items.removeAllElements();
        selected = -1;
        top_index = 0;
        buttom_index = 0;
        full_screen = 0;
    }

    public void addElement(Element e)
    {
        items.addElement(e);
        full_screen += e.height;
        if(items.size() > 1)
        {
            if(e.top < screen_height - 1)
                buttom_index++;
        } else
        {
            top_index = 0;
            if(e.selectable)
                selected = 0;
            buttom_index = 1;
        }
        canvas.redraw(1);
    }

    public void addElement(Element e, int s)
    {
        items.addElement(e);
        full_screen += e.height;
        if(items.size() > s)
        {
            if(e.top < screen_height - 1)
            {
                buttom_index++;
                canvas.redraw(1);
            }
        } else
        if(items.size() == s)
        {
            top_index = items.size() - 1;
            if(e.selectable)
                selected = top_index;
            buttom_index = top_index + 1;
            canvas.redraw(1);
        }
    }

    public void gotoEnd()
    {
        if(items.size() == 0)
            return;
        buttom_index = items.size() - 1;
        Element itm = (Element)items.elementAt(buttom_index);
        if(itm.top + itm.height <= screen_height)
        {
            top_index = 0;
            top = 0;
            return;
        }
        int h = 0;
        top_index = buttom_index;
        do
        {
            h += itm.height;
            if(h >= screen_height)
                break;
            itm = (Element)items.elementAt(--top_index);
        } while(top_index > 0);
        top_index += 2;
        itm = (Element)items.elementAt(--top_index);
        top = 0 - itm.top;
    }

    public int getWidth()
    {
        return width;
    }

    public int getTop()
    {
        int t = top + 16383;
        return top_index << 15 | t;
    }

    public void setTop(int i)
    {
        int topi = i >> 15;
        int topy = (i & 0x7fff) - 16383;
        if(topi < 0 || topi >= items.size())
            return;
        buttom_index = top_index = topi;
        Element itm = (Element)items.elementAt(buttom_index);
        if(itm.top + itm.height <= screen_height)
        {
            top = 0;
            return;
        }
        int h = 0;
        do
        {
            h += itm.height;
            System.out.println("Page.setTop():" + h + "," + itm.height);
            if(h >= screen_height || buttom_index + 1 >= items.size())
                break;
            itm = (Element)items.elementAt(++buttom_index);
        } while(true);
        buttom_index++;
        if(buttom_index < items.size())
        {
            buttom_index++;
            if(buttom_index < items.size())
                buttom_index++;
        }
        top = topy;
        canvas.redraw(1);
        System.out.println("Page.setTop():" + top + "," + top_index + "," + buttom_index + "," + screen_height);
    }

    public void run()
    {
        img_cur = Image.createImage(width - 3, screen_height);
        Graphics g = img_cur.getGraphics();
        //drawPage(g, 0, 0, screen_height);
        drawPage(g, 0, 20, screen_height);
        buttom_index = next_buttom;
        top_index = next_topidx;
        img_next = Image.createImage(width - 3, SCROLL_HEIGHT);
        g = img_next.getGraphics();
        anim_div = anim_y * 10;
        if(anim_div < 0)
        {
            anim_y = 0;
            top -= screen_height;
            drawPage(g, 0, 0, screen_height);
            top = next_top;
        } else
        {
            top = next_top;
            anim_y = 0 - SCROLL_HEIGHT;
            drawPage(g, 0, 0, SCROLL_HEIGHT);
        }
        do
        {
            anim_y += anim_div;
            if(anim_div >= 0 ? anim_y <= 0 : anim_y >= 0 - SCROLL_HEIGHT)
            {
                canvas.redraw(1);
                try
                {
                    Thread.sleep(100L);
                }
                catch(InterruptedException e) { }
            } else
            {
                selected = next_selected;
                img_cur = null;
                img_next = null;
                anim_div = 0;
                canvas.redraw(1);
                return;
            }
        } while(true);
    }

    protected Vector items;
    protected int top;
    public boolean dontdrawscrollbar = false;
    public int SCROLL_HEIGHT;
    protected int screen_height;
    protected int full_screen;
    protected int selected;
    int top_index;
    protected int buttom_index;
    protected int width;
    protected BrowserCanvas canvas;
    protected PageListener listener;
    public int bgcolor;
    public String right_menu;
    public Object left_menu;
    private Image img_cur;
    private Image img_next;
    private int anim_y;
    private int anim_div;
    private int next_top;
    private int next_topidx;
    private int next_buttom;
    private int next_selected;
    protected boolean animated;
    static Image icon_up;
    static Image icon_down;
}
